<template>
  <el-dialog
    v-el-drag-dialog
    title="添加设备"
    :visible.sync="dialogVisible"
    width="60%"
  >
    <el-form ref="mainForm" size="medium" :model="formData" label-width="100px">
      <el-form-item label="选择设备">
        <el-transfer
          v-model="formData.devices"
          :titles="titles"
          :props="deviceProps"
          :data="devices"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submit('mainForm')">提交</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import { mapGetters } from 'vuex'
export default {
  props: {
    devicesObj: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    return {
      devices: [],
      formData: {
        devices: []
      },
      deviceProps: {
        key: 'serialno',
        label: 'name',
        disabled: 'disabled'
      },
      titles: ['可添加设备', '已添加设备'],
      dialogVisible: false
    }
  },
  computed: {
    ...mapGetters(['project'])
  },
  watch: {
    devicesObj: {
      handler(newVal) {
        if (newVal.devices) {
          this.devices = newVal.devices
          this.formData.devices = newVal.devices_exist.map(val => val.serialno)
        }
      },
      deep: true,
      immediate: true
    }
  },
  methods: {
    show() {
      this.dialogVisible = true
    },
    hide() {
      this.dialogVisible = false
    },
    submit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$emit('addSubmit', this.formData)
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
